﻿using MediatR;
using SmartProduction.Domain;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Read.API.Application.Commands.BomCommand;

namespace SmartProduction.Read.API.Application.CommandHandlers.BomCommandHandler
{
    public class MaterialListCommandHandler : IRequestHandler<MaterialListCommand, APIResult<List<MaterListq>>>
    {/// <summary>
     /// 工艺流程仓储
     /// </summary>
        private readonly IBaseRepository<MaterListq> partsRepository;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="mapper">映射</param>
        /// <param name="procRepository">bom配件仓储</param>
        public MaterialListCommandHandler(IBaseRepository<MaterListq> partsRepository)
        {
            this.partsRepository = partsRepository;
        }
        public Task<APIResult<List<MaterListq>>> Handle(MaterialListCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<MaterListq>> result = new APIResult<List<MaterListq>>();
            var entity = partsRepository.GetAll().Result;
            if (request.MaterialListTypeId != 0)
            {
                entity = entity.Where(x => x.MaterialListTypeId == request.MaterialListTypeId);
            }
            result.Code = APIEnums.查询成功;
            result.Msg = APIEnums.查询成功.ToString();
            result.data = entity.ToList();
            return Task.FromResult(result);
        }
    }
}
